(function() {
  var express, iced, pgsql, router, sendmail, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  pgsql = require('pg');

  sendmail = require('../lib/sendmail');

  router = express.Router();

  module.exports = router;

  router.all("/", function(req, res, next) {
    var client, done, err, index, params, result, sql, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.breadcrumbs.push({
      'title': '站内通知',
      'href': "/notify"
    });
    res.locals.breadcrumbs.push({
      'title': '站内留言'
    });
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/message.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 15
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/message.coffee"
          });
          client.query("select u.*, count(m.serial) as n_messages from web_users as u left join web_messages as m on (m.recver = u.uuid or m.sender = u.uuid) where u.uuid <> $1 and (m.recver = $1 or m.sender = $1) group by u.serial, u.uuid order by n_messages", [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 27
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.users = result.rows;
          params = [];
          sql = "select m.*, u.realname as send_user, u2.realname as recv_user from web_messages as m left join web_users as u on u.uuid = m.sender left join web_users as u2 on u2.uuid = m.recver where (m.recver = $1 or m.sender = $1)";
          params.push(req.session.userinfo.uuid);
          index = 2;
          if (req.body.start_time && req.body.start_time.length > 0) {
            sql += " and m.time >= $" + index;
            index += 1;
            params.push(req.body.start_time);
            res.locals.start_time = req.body.start_time;
          }
          if (req.body.end_time && req.body.end_time.length > 0) {
            sql += " and time <= $" + index;
            index += 1;
            params.push(req.body.end_time);
            res.locals.end_time = req.body.end_time;
          }
          if (req.body.user) {
            sql += " and (recver = $" + index + " or sender = $" + index + ")";
            index += 1;
            params.push(req.body.user);
            res.locals.user = req.body.user;
          } else if (req.query.user) {
            sql += " and (recver = $" + index + " or sender = $" + index + ")";
            index += 1;
            params.push(req.query.user);
            res.locals.user = req.query.user;
          }
          sql += " order by time desc";
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/message.coffee"
            });
            client.query(sql, params, __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 62
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            done();
            return res.render('message', {
              records: result.rows
            });
          });
        });
      };
    })(this));
  });

  router.post('/new', function(req, res, next) {
    var client, done, err, k, redirect, result, user, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    _ref = ['user', 'message'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/message.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 72
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/message.coffee"
          });
          client.query("insert into web_messages (sender, recver, message) values ( $1, $2, $3 )", [req.session.userinfo.uuid, req.body.user, req.body.message], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 82
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          req.app.set('notify_modified_time', moment().format('x'));
          redirect = req.body.redirect === 'message' ? '/message' : '/notify';
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/message.coffee"
            });
            client.query("select * from web_session where sess#>>'{userinfo, uuid}' = $1", [req.body.user], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 94
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            if (result.rows.length > 0) {
              done();
              return res.redirect(redirect);
            }
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/message.coffee"
              });
              client.query("select * from web_users where uuid = $1", [req.body.user], __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    err = arguments[0];
                    return result = arguments[1];
                  };
                })(),
                lineno: 104
              }));
              __iced_deferrals._fulfill();
            })(function() {
              if (err) {
                done(client);
                return next(err);
              }
              done();
              if (result.rows.length === 0) {
                return res.redirect(redirect);
              }
              user = result.rows[0];
              if (!user.email || user.email.length === 0) {
                return res.redirect(redirect);
              }
              sendmail({
                subject: "来自'" + req.session.userinfo.realname + "'的站内留言",
                html: req.body.message,
                to: "\"" + user.realname + "\" " + user.email
              });
              return res.redirect(redirect);
            });
          });
        });
      };
    })(this));
  });

  router.post('/read', function(req, res, next) {
    var client, done, err, k, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    _ref = ['serial', 'recver'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/message.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 128
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/message.coffee"
          });
          client.query("update web_messages set status = 2 where serial = $1 and recver = $2", [req.body.serial, req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 135
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          req.app.set('notify_modified_time', moment().format('x'));
          return res.json({
            succ: true
          });
        });
      };
    })(this));
  });

  router.post('/delete', function(req, res, next) {
    var client, done, err, k, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    _ref = ['serial'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/message.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 149
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/message.coffee"
          });
          client.query("delete from web_messages where serial = $1 and (sender = $2 or recver = $2)", [req.body.serial, req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 157
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          req.app.set('notify_modified_time', moment().format('x'));
          return res.json({
            succ: true
          });
        });
      };
    })(this));
  });

}).call(this);
